<?php
/**
 * 生成xml库
 */

/*<?xml version="1.0" encoding="UTF-8"?>
<ceb:CEB311Message guid="76BA721E-FE4D-E8D9-8759-18D640BB9679" version="1.0"  xmlns:ceb="http://www.chinaport.gov.cn/ceb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<ceb:Order>
		<ceb:OrderHead>
			<ceb:guid>76BA721E-ED4D-E8D9-8759-18D640BB9649</ceb:guid>
			<ceb:appType>1</ceb:appType>
			<ceb:appTime>20181120050632</ceb:appTime>
			<ceb:appStatus>2</ceb:appStatus>
			<ceb:orderType>I</ceb:orderType>
            <ceb:orderNo>order12018112011192060122</ceb:orderNo>
			<ceb:ebpCode>4403160CNY</ceb:ebpCode>
			<ceb:ebpName>深圳市前海雅虎国际贸易有限公司</ceb:ebpName>
			<ceb:ebcCode>4403160CNY</ceb:ebcCode>
			<ceb:ebcName>深圳市前海雅虎国际贸易有限公司</ceb:ebcName>
			<ceb:goodsValue>89.93</ceb:goodsValue>
			<ceb:freight>5000</ceb:freight>
			<ceb:discount>0</ceb:discount>
			<ceb:taxTotal>70</ceb:taxTotal>
			<ceb:acturalPaid>89.93</ceb:acturalPaid>
			<ceb:currency>142</ceb:currency>
			<ceb:buyerRegNo>ID20160001</ceb:buyerRegNo>
			<ceb:buyerName>承燕</ceb:buyerName>
			<ceb:buyerIdType>1</ceb:buyerIdType>
			<ceb:buyerIdNumber>342923198409042623</ceb:buyerIdNumber>
            <ceb:buyerTelephone>18616596664</ceb:buyerTelephone>
			<ceb:payCode>312226T001</ceb:payCode>
			<ceb:payName>支付宝(中国)网络技术有限公司</ceb:payName>
			<ceb:payTransactionId>20180001634226001</ceb:payTransactionId>
			<ceb:batchNumbers>20180317</ceb:batchNumbers>
			<ceb:consignee>焦洪宇</ceb:consignee>
			<ceb:consigneeTelephone>18616596664</ceb:consigneeTelephone>
			<ceb:consigneeAddress>浙江省 杭州市 建德市</ceb:consigneeAddress>
			<ceb:consigneeDistrict>072750</ceb:consigneeDistrict>
			<ceb:note>test</ceb:note>
		</ceb:OrderHead>
		<ceb:OrderList>
			<ceb:gnum>1</ceb:gnum>
			<ceb:itemNo>AF001-001</ceb:itemNo>
			<ceb:itemName>Linden Leaves</ceb:itemName>
            <ceb:gmodel>Linden Leaves梦幻薰</ceb:gmodel>
			<ceb:itemDescribe>Linden Leaves</ceb:itemDescribe>
			<ceb:barCode>2345123</ceb:barCode>
			<ceb:unit>007</ceb:unit>
			<ceb:qty>100</ceb:qty>
			<ceb:price>20</ceb:price>
			<ceb:totalPrice>200</ceb:totalPrice>
			<ceb:currency>142</ceb:currency>
			<ceb:country>116</ceb:country>
			<ceb:note></ceb:note>
		</ceb:OrderList>
	</ceb:Order>
	<ceb:BaseTransfer>
		<ceb:copCode>4403160CNY</ceb:copCode>
		<ceb:copName>深圳市前海雅虎国际贸易有限公司</ceb:copName>
		<ceb:dxpMode>DXP</ceb:dxpMode>
		<ceb:dxpId>DXPENT0000020457</ceb:dxpId>
		<ceb:note>test</ceb:note>
	</ceb:BaseTransfer>	
</ceb:CEB311Message>*/
function generate_xml($data_arr){
    /* xml方式 */
    if (class_exists('DOMDocument'))
    {
        $doc = new DOMDocument('1.0','UTF-8');
        $doc->formatOutput=true;

        $node_Message = $doc->createElement('ceb:CEB311Message');
        $node_Message->setAttribute( "guid", $data_arr['Order']['OrderHead']['guid'] );
        $node_Message->setAttribute( "version", "1.0" );
        $node_Message->setAttribute( "xmlns:ceb", "http://www.chinaport.gov.cn/ceb" );
        $node_Message->setAttribute( "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance" );

			$node_Order = $doc->createElement('ceb:Order');

				$node_OrderHead = $doc->createElement('ceb:OrderHead');
				foreach ($data_arr['Order']['OrderHead'] as $_key => $_row) {
					$node_temp = $doc->createElement('ceb:'.$_key,$_row);
					$node_OrderHead->appendChild($node_temp);
	        	}
				$node_Order->appendChild($node_OrderHead);

				foreach ($data_arr['Order']['OrderList'] as $_key => $_arr) {
					$node_OrderList = $doc->createElement('ceb:OrderList');
					foreach ($_arr as $arr_key => $arr_row) {
						$node_temp = $doc->createElement('ceb:'.$arr_key,$arr_row);
						$node_OrderList->appendChild($node_temp);
					}
					$node_Order->appendChild($node_OrderList);
	        	}
	        	
			$node_Message->appendChild($node_Order);

			$node_BaseTransfer = $doc->createElement('ceb:BaseTransfer');
			foreach ($data_arr['BaseTransfer'] as $_key => $_row) {
				$node_temp = $doc->createElement('ceb:'.$_key,$_row);
				$node_BaseTransfer->appendChild($node_temp);
			}
			$node_Message->appendChild($node_BaseTransfer);

		$doc->appendChild($node_Message);

        return $doc->saveXML();
    }else{
	    return ('<?xml version="1.0" encoding="UTF-8"?>' . array2xml($data_arr, 'ceb:CEB311Message'));
    }

}

/**
 *  循环生成xml节点
 *
 *  @param  handle      $doc            xml实例句柄
 *  @param  handle      $top            当前父节点
 *  @param  array       $info_arr       需要解析的数组
 *  @param  boolean     $have_item      是否是数据数组，是则需要在每条数据上加item父节点
 *
 */
function create_tree($doc, $top, $info_arr, $have_item = false)
{
    if (is_array($info_arr))
    {
        foreach ($info_arr as $key => $val)
        {
            if (is_array($val))
            {
                if ($have_item == false)
                {
					$data_info=$doc->createElement('data_info');
					$top->appendChild($data_info);
					create_tree($doc, $data_info, $val, true);
                }
                else
                {
					$item=$doc->createElement('item');
					$top->appendChild($item);
					$key_code = $doc->createAttribute('key');
					$item->appendChild($key_code);
					$key_code->appendChild($doc->createTextNode($key));
					create_tree($doc, $item, $val);
                }
            }
            else
            {
                $text_code=$doc->createElement($key);
                $top->appendChild($text_code);
                if (is_string($val))
                {
					$text_code->appendChild($doc->createCDATASection($val));
                }
                else
                {
					$text_code->appendChild($doc->createTextNode($val));
                }
            }
        }
    }
    else
    {
        $top->appendChild($doc->createCDATASection($info_arr));
    }
}

function array2xml($data,$root='Message'){
    $xml='<'.$root.'>';
    _array2xml($data,$xml);
    $xml.='</'.$root.'>';
    return $xml;
}

function _array2xml(&$data,&$xml){
    if(is_array($data)){
        foreach($data as $k=>$v){
            if(is_numeric($k)){
                $xml.='<item key="' . $k . '">';
                $xml.=_array2xml($v,$xml);
                $xml.='</item>';
            }else{
                $xml.='<'.$k.'>';
                $xml.=_array2xml($v,$xml);
                $xml.='</'.$k.'>';
            }
        }
    }elseif(is_numeric($data)){
        $xml.=$data;
    }elseif(is_string($data)){
        $xml.='<![CDATA['.$data.']]>';
    }
}

?>